package com.wfg.psi;

import com.foxsteps.gsonformat.entity.ClassEntity;
import com.foxsteps.gsonformat.entity.FieldEntity;
import com.foxsteps.gsonformat.tools.checktreetable.FiledTreeTableModel;
import com.foxsteps.gsonformat.tools.json.JSONObject;
import com.foxsteps.gsonformat.tools.ux.CheckTreeTableManager;
import com.google.gson.JsonParser;
import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

import static javax.swing.ListSelectionModel.SINGLE_SELECTION;

public class FieldsDialog extends JFrame {
    private JPanel contentPane;
    private JPanel filedPanel;
    private JButton noBt;
    private JButton okBt;
    private JPanel buttonPanel;
    private JScrollPane jScrollPanel;
    private ArrayList<DefaultMutableTreeTableNode> defaultMutableTreeTableNodeList = new ArrayList<>();;

    public FieldsDialog() {
        setContentPane(contentPane);
        setTitle("Virgo Model");
        getRootPane().setDefaultButton(okBt);
        this.setAlwaysOnTop(true);
        initListener();
    }
    private void initListener() {
        // 创建 JXTreeTable
        // FiledTreeTableModel FiledTreeTableModel
        JXTreeTable treetable = new JXTreeTable(new FiledTreeTableModel(createData()));
        CheckTreeTableManager manager = new CheckTreeTableManager(treetable);
        manager.getSelectionModel().addPathsByNodes(defaultMutableTreeTableNodeList);
        treetable.getColumnModel().getColumn(0).setPreferredWidth(150);
//        treetable.setSelectionBackground(treetable.getBackground());
        treetable.expandAll();
        treetable.setCellSelectionEnabled(false);
        final DefaultListSelectionModel defaultListSelectionModel = new DefaultListSelectionModel();
        treetable.setSelectionModel(defaultListSelectionModel);

        defaultListSelectionModel.setSelectionMode(SINGLE_SELECTION);
        defaultListSelectionModel.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                defaultListSelectionModel.clearSelection();
            }
        });
//        defaultMutableTreeTableNodeList = null;
        treetable.setRowHeight(30);
        jScrollPanel.setViewportView(treetable);
//        generateClass.setText(generateClassStr);
        okBt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });
        noBt.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });
        contentPane.registerKeyboardAction(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        contentPane.registerKeyboardAction(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }
    private void onCancel(){
        System.out.println("no");
    }
    private void onOK(){
        DefaultMutableTreeTableNode defaultMutableTreeTableNode = defaultMutableTreeTableNodeList.get(0);
        System.out.println(defaultMutableTreeTableNode);
        System.out.println(defaultMutableTreeTableNode.getColumnCount());
        Object valueAt = defaultMutableTreeTableNode.getValueAt(0);
        System.out.println(JSONObject.valueToString(valueAt));
        System.out.println(defaultMutableTreeTableNode.getUserObject().toString());
        System.out.println(JSONObject.valueToString(defaultMutableTreeTableNode));

        System.out.println("ok");
    }

    private DefaultMutableTreeTableNode createData() {
        ClassEntity classEntity = new ClassEntity();
        classEntity.setClassName("111");
        classEntity.setClassDesc("desc");
        FieldEntity fieldEntity = new FieldEntity();
        fieldEntity.setFieldName("abc");
        fieldEntity.setType("string");
        ((List<FieldEntity>) classEntity.getFields()).add(fieldEntity);

        DefaultMutableTreeTableNode root = new DefaultMutableTreeTableNode(classEntity);
        createDataNode(root, classEntity);
        return root;
    }

    private void createDataNode(DefaultMutableTreeTableNode root, ClassEntity innerClassEntity) {
        for (FieldEntity field : innerClassEntity.getFields()) {
            DefaultMutableTreeTableNode node = new DefaultMutableTreeTableNode(field);
            defaultMutableTreeTableNodeList.add(node);
            root.add(node);
        }
        for (ClassEntity classEntity : innerClassEntity.getInnerClasses()) {
            DefaultMutableTreeTableNode node = new DefaultMutableTreeTableNode(classEntity);
            root.add(node);
            createDataNode(node, classEntity);
        }

    }
}
